package theList;

/**
 * @auther wangchenghao
 * @date 2021/6/24
 * 如何检测一个链表是否有环
 **/
public class test6 {
    public boolean hasCycle(ListNode head) {
        ListNode fast=head;
        ListNode slow=head;
        if(fast==null){
            return  false;
        }
        while (fast!=null&& fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if (fast==slow){
                return true;
            }
        }
        //到结尾时
        return !(fast==null||fast.next==null);
    }
}
